/*
 * 作者：姚俊
 * 功能：学科的业务处理
 * 日期：2015-05-21
 */
package admin.service.subject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import database.dao.MysqlCrud;
import admin.bean.Subject;

public class SubjectService {	
	/**
	 * @category 根据学科项目的ID来查询一个学科
	 * @author yaojun
	 * @param subject Subject 传入一个学科信息的对象，该对象必须有学科信息的ID
	 * @return Subject 返回的是一个学科信息的对象
	 */
	public Subject getSubject(Subject subject)
	{
		Subject sub =new Subject();
		String sql="select * from Subject where subId=?";
		String[]  parameter={String.valueOf(subject.getSubId())};
        MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		if(lists.size()>0&&lists!=null)
		{	
			Map map=lists.get(0);
			try {			
				sub.setSubId(Integer.parseInt(map.get(0).toString()));
				sub.setName(map.get(1).toString());
				sub.setUniversity(map.get(2).toString());
				sub.setArea(map.get(3).toString());
				sub.setChargePerson(map.get(4).toString());
				sub.setMainWeb(map.get(5).toString());
				sub.setEmail(map.get(6).toString());
				sub.setSuperLevelDate(map.get(7).toString());
				sub.setMainDirection(map.get(8).toString());
				sub.setRemark(map.get(9).toString());
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}	
		return sub;
	}
	/**
	 * @category 根据学科的ID来删除一个学科记录
	 * @author yaojun
	 * @param subject Subject 传入一个学科信息的对象
	 * @return boolean 当删除成功返回true，否则返回false
	 */
	public boolean deleteSubject(Subject subject)
	{
		boolean b=false;
		String sql="delete from Subject where subId=?";
		String[]  parameter={String.valueOf(subject.getSubId())};
		 MysqlCrud mysqlcurd=new MysqlCrud();
		b=mysqlcurd.InsertAndUpdateData(sql, parameter);
		 return b;
	}
	
	/**
	 * @category 插入一个学科信息对象数据
	 * @author yaojun
	 * @param subject Subject 传入一个学科信息对象的参数
	 * @return boolean 如果成功true
	 */
	public boolean insertSubject(Subject subject)
	{
		boolean b=false;
		String sql="insert into Subject(name,university," +
		"area,chargePerson,mainWeb,email,superLevelDate," +
		"mainDirection,remark)"+" values(?,?,?,?,?,?,?,?,?);";
		String[] parameter={String.valueOf(subject.getName()),
				String.valueOf(subject.getUniversity()),String.valueOf(subject.getArea()),
				String.valueOf(subject.getChargePerson()),String.valueOf(subject.getMainWeb()),
				String.valueOf(subject.getEmail()),String.valueOf(subject.getSuperLevelDate()),
				String.valueOf(subject.getMainDirection()),String.valueOf(subject.getRemark()),};	
		MysqlCrud mysqlcurd=new MysqlCrud();
		b=mysqlcurd.InsertAndUpdateData(sql, parameter);
		return b;
	}
	
	/**
	 * @category 根据学科信息的ID来修改这个学科的信息
	 * @author yaojun
	 * @param sub1 Subject 传入一个要用于修改的Subject对象
	 * @param sub2 Subject 传入一个根据这个Subject对象的ID来查找需要修改的哪一条学科信息数据
	 * @return boolean 成功则返回true
	 */
	public boolean updateSubject(Subject sub1,Subject sub2)
	{
		boolean b=false;
		String sql="update Subject set name=?,university=?,area=?," +
		"chargePerson=?,mainWeb=?,email=?,superLevelDate=?," +
		"mainDirection=?,remark=? where subId=?";
		
		String[] parameter={String.valueOf(sub1.getName()),String.valueOf(sub1.getUniversity()),
				String.valueOf(sub1.getArea()),String.valueOf(sub1.getChargePerson()),
				String.valueOf(sub1.getMainWeb()),String.valueOf(sub1.getEmail()),
				String.valueOf(sub1.getSuperLevelDate()),String.valueOf(sub1.getMainDirection()),
				String.valueOf(sub1.getRemark()),String.valueOf(sub2.getSubId())};
		
		MysqlCrud mysqlcurd=new MysqlCrud();
		b=mysqlcurd.InsertAndUpdateData(sql, parameter);
		return b;
	}
	
	/**
	 * @category 得到学科信息的数量
	 * @author yaojun
	 * @return int
	 */
	public int getCountSubject()
	{
		int sum=0;
		String sql="select count(*) from Subject";
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,null);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 根据名称的条件查询所有学科信息
	 * @author yaojun
	 * @param subject Subject 传入一个学科管理信息的对象，该对象中必须有学科管理信息的名称
     * @return 返回一个类型为Subject的List集合
	 */
	public List<Subject> getAllSubjectByName(Subject subject)
	{
		String sql="select * from Subject where name like '%"+subject.getName()+"%'";
		List<Subject> subjects=new ArrayList<Subject>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,null);
		for(int i=0;i<lists.size();i++)
		{
			Map map=lists.get(i);
           try {
				Subject sub=new Subject();
				sub.setSubId(Integer.parseInt(map.get(0).toString()));
				sub.setName(map.get(1).toString());
				sub.setUniversity(map.get(2).toString());
				sub.setArea(map.get(3).toString());
				sub.setChargePerson(map.get(4).toString());
				sub.setMainWeb(map.get(5).toString());
				sub.setEmail(map.get(6).toString());
				sub.setSuperLevelDate(map.get(7).toString());
				sub.setMainDirection(map.get(8).toString());
				sub.setRemark(map.get(9).toString());
				subjects.add(sub);				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}			
		}
		return subjects;		
	}	
	
	/**
	 * @category 查询所有学科信息
	 * @author yaojun
     * @return 返回一个类型为Subject的List集合
	 */
	public List<Subject> getAllSubject()
	{
		String sql="select * from Subject";
		List<Subject> subjects=new ArrayList<Subject>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,null);
		for(int i=0;i<lists.size();i++)
		{
			Map map=lists.get(i);
           try {
				Subject sub=new Subject();
				sub.setSubId(Integer.parseInt(map.get(0).toString()));
				sub.setName(map.get(1).toString());
				sub.setUniversity(map.get(2).toString());
				sub.setArea(map.get(3).toString());
				sub.setChargePerson(map.get(4).toString());
				sub.setMainWeb(map.get(5).toString());
				sub.setEmail(map.get(6).toString());
				sub.setSuperLevelDate(map.get(7).toString());
				sub.setMainDirection(map.get(8).toString());
				sub.setRemark(map.get(9).toString());
				subjects.add(sub);				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}			
		}
		return subjects;		
	}	
}
